package ch.ra.dev.nastywheel;

import java.util.ArrayList;
import java.util.List;

import ch.ra.dev.nastywheel.activity.ReadContactActivity;
import ch.ra.dev.nastywheel.util.Contact;
import ch.ra.dev.nastywheel.util.Keys;
import ch.ra.dev.nastywheel.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class ContactActivity extends Activity {
	/**
	 * Whether or not the system UI should be auto-hidden after
	 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
	 */
	private static final boolean AUTO_HIDE = true;

	/**
	 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
	 * user interaction before hiding the system UI.
	 */
	private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

	/**
	 * If set, will toggle the system UI visibility upon interaction. Otherwise,
	 * will show the system UI visibility upon interaction.
	 */
	private static final boolean TOGGLE_ON_CLICK = true;

	/**
	 * The flags to pass to {@link SystemUiHider#getInstance}.
	 */
	private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

	/**
	 * The instance of the {@link SystemUiHider} for this activity.
	 */
	private SystemUiHider mSystemUiHider;
	
	private List<Contact> contacts = new ArrayList<Contact>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_contact);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);	

		readContacts();
	}
	
	private void readContacts() {
		contacts.clear();
		
		registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
            	Bundle contactsBundle = arg1.getBundleExtra(Keys.CONTACTS_BUNDLE);
            	
            	ArrayList<Parcelable> contactParcelable = contactsBundle.getParcelableArrayList(Keys.CONTACTS);

            	for(Parcelable contact : contactParcelable) {
            		contacts.add((Contact)contact);
            	}

        		final View controlsView = findViewById(R.id.contact_content_controls);
        		createContactView((LinearLayout)controlsView);
            }
		}, new IntentFilter(Keys.READ_CONTACTS));
		
		final Intent ReadContactIntent = new Intent(ContactActivity.this, ReadContactActivity.class);
		ContactActivity.this.startActivity(ReadContactIntent);
	}
	
	private void createContactView(LinearLayout controlsView) {
		LayoutParams lparams = new LayoutParams(
				   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		for(Contact c : contacts) {
			TextView contactNameTextView = new TextView(this);
			contactNameTextView.setText(c.getName() + ": " + c.getContactData());
			contactNameTextView.setLayoutParams(lparams);
			
			controlsView.addView(contactNameTextView);
		}
	}
	
}
